package com.hanko.fastdfs;

import lombok.SneakyThrows;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Arrays;

/**
 * @author chengdu
 * @date 2019/7/13.
 */
public class FdfsTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(FdfsTest.class);

    private static final String CONF_NAME = "fdfs_client.conf";




    @SneakyThrows
    public static void main(String[] args) {
        ClientGlobal.init(CONF_NAME);
        upload2();
    }


    /**
     * 方案一：通过设置storage的group来实现不同项目不同存储
     * @throws Exception
     */
    public static void upload1() throws Exception{
        TrackerClient tracker = new TrackerClient();
        TrackerServer trackerServer = tracker.getTrackerServer();
        StorageServer storageServer = tracker.getStoreStorage(trackerServer,"group1");
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        NameValuePair[] metaList = new NameValuePair[1];
        String local_filename = "1.PNG";
        metaList[0] = new NameValuePair("fileName", local_filename);
        File file = new File("D:/data/Desktop/1/1.png");
        InputStream inputStream = new FileInputStream(file);
        int length = inputStream.available();
        byte[] bytes = new byte[length];
        inputStream.read(bytes);
        String[] result = storageClient.upload_file(bytes, "png", metaList);
        LOGGER.info("result {}", Arrays.asList(result));
        System.out.println(storageClient.getTrackerServer().getInetSocketAddress());
        try {
            System.out.println(storageClient.getStorageServer().getInetSocketAddress());
        }catch (Exception e){

        }

    }


    /**
     * 方案二：通过设置new  StorageServer实现不同项目不同存储
     * @throws Exception
     */
    public static void upload2() throws Exception{
        TrackerClient tracker = new TrackerClient();
        TrackerServer trackerServer = tracker.getTrackerServer();
        StorageServer storageServer = new StorageServer("192.168.51.212", 23000, 0);
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        NameValuePair[] metaList = new NameValuePair[1];
        String local_filename = "1.PNG";
        metaList[0] = new NameValuePair("fileName", local_filename);
        File file = new File("D:/data/Desktop/1/1.png");
        InputStream inputStream = new FileInputStream(file);
        int length = inputStream.available();
        byte[] bytes = new byte[length];
        inputStream.read(bytes);
        String[] result = storageClient.upload_file(bytes, "png", metaList);
        LOGGER.info("result {}", Arrays.asList(result));
        System.out.println(storageClient.getTrackerServer().getInetSocketAddress());
        try {
            System.out.println(storageClient.getStorageServer().getInetSocketAddress());
        }catch (Exception e){

        }

    }


    public void download() throws Exception {
        StorageClient storageClient = null;
        String[] uploadresult = {"group1", "M00/3E/01/wKgz1GEKD1mAeaKZAAzcpb7d4DA6963949"};
        byte[] result = storageClient.download_file(uploadresult[0], uploadresult[1]);
        String local_filename = "build.PNG";
        writeByteToFile(result, local_filename);
        File file = new File(local_filename);
    }


    public void writeByteToFile(byte[] fbyte, String fileName) throws IOException {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = new File(fileName);
        try {
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(fbyte);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                bos.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }

}
